package com.pet.system.config;

import com.pet.system.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class JWTInterceptor implements HandlerInterceptor {
    public JWTInterceptor(TokenService tokenService) {
        this.tokenService = tokenService;
    }

    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //从请求头内获取token
        String token = request.getHeader("authorization");
        // 是否登出
        tokenService.getLoginCacheToken(token);
        //验证令牌  如果令牌不正确会出现异常 被全局异常处理
        tokenService.verify(token);

        return true;
    }

}

